﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Host;
using System.Threading;
using System.Management.Automation;
using System.Collections.ObjectModel;
using System.Security;
using System.Text.RegularExpressions;

namespace Decav.Web.PowerShell
{
    public class PowershellWebUserInterface : PSHostUserInterface
    {
        private HttpContext _context;
        private RawUserInterface _rawUi;

        public PowershellWebUserInterface(HttpContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            _context = context;
            _rawUi = new RawUserInterface(context);
        }

        public override Dictionary<string, PSObject> Prompt(string caption, string message, System.Collections.ObjectModel.Collection<FieldDescription> descriptions)
        {
            throw new NotImplementedException("PowerShell HTML does not support PowerShell prompts.");
        }

        public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
        {
            throw new NotImplementedException("PowerShell HTML does not support PowerShell prompts.");
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, System.Management.Automation.PSCredentialTypes allowedCredentialTypes, System.Management.Automation.PSCredentialUIOptions options)
        {
            throw new NotImplementedException("PowerShell HTML does not support PowerShell prompts.");
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
        {
            throw new NotImplementedException("PowerShell HTML does not support PowerShell prompts.");
        }

        public override PSHostRawUserInterface RawUI
        {
            get { return _rawUi; }
        }

        public override string ReadLine()
        {
            throw new NotImplementedException("PowerShell HTML does not support PowerShell line reads.");
        }

        public override SecureString ReadLineAsSecureString()
        {
            throw new NotImplementedException("PowerShell HTML does not support PowerShell line reads.");
        }

        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            Write(value);
        }

        public override void Write(string value)
        {
            _context.Response.Output.Write(value);
        }

        public override void WriteDebugLine(string message)
        {
            WriteLine(string.Format("DEBUG: ", message));
        }

        public override void WriteErrorLine(string value)
        {
            WriteLine(string.Format("ERROR: ", value));
        }

        public override void WriteLine(string value)
        {
            _context.Response.Output.WriteLine(value);
        }

        public override void WriteProgress(long sourceId, System.Management.Automation.ProgressRecord record)
        {
        }

        public override void WriteVerboseLine(string message)
        {
            WriteLine(message);
        }

        public override void WriteWarningLine(string message)
        {
            WriteLine(string.Format("WARNING: ", message));
        }

        private class RawUserInterface : PSHostRawUserInterface
        {
            private HttpContext _context;

            public RawUserInterface(HttpContext context)
            {
                if (context == null)
                    throw new ArgumentNullException("context");

                _context = context;
            }

            public override ConsoleColor BackgroundColor
            {
                get
                {
                    return ConsoleColor.White;
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            public override Size BufferSize
            {
                get
                {
                    return new Size(2000, 2000);
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            public override Coordinates CursorPosition
            {
                get
                {
                    return new Coordinates(0, 0);
                }
                set
                {
                }
            }

            public override int CursorSize
            {
                get
                {
                    return 0;
                }
                set
                {
                }
            }

            public override void FlushInputBuffer()
            {
            }

            public override ConsoleColor ForegroundColor
            {
                get
                {
                    return ConsoleColor.Black;
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            public override BufferCell[,] GetBufferContents(Rectangle rectangle)
            {
                throw new NotImplementedException();
            }

            public override bool KeyAvailable
            {
                get { return false; }
            }

            public override Size MaxPhysicalWindowSize
            {
                get { return new Size(2000, 2000); }
            }

            public override Size MaxWindowSize
            {
                get { return new Size(2000, 2000); }
            }

            public override KeyInfo ReadKey(ReadKeyOptions options)
            {
                throw new NotImplementedException();
            }

            public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill)
            {
                throw new NotImplementedException();
            }

            public override void SetBufferContents(Rectangle rectangle, BufferCell fill)
            {
                throw new NotImplementedException();
            }

            public override void SetBufferContents(Coordinates origin, BufferCell[,] contents)
            {
                throw new NotImplementedException();
            }

            public override Coordinates WindowPosition
            {
                get
                {
                    return new Coordinates(0, 0);
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            public override Size WindowSize
            {
                get
                {
                    return new Size(10000, 10000);
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            public override string WindowTitle
            {
                get
                {
                    return "PowerShell HTTP Request";
                }
                set
                {
                }
            }
        }
    }
}
